/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.GUI;

import edu.nii.qlks.ControlLayer.CTPhieuDenControl;
import edu.nii.qlks.ControlLayer.KhachHangControl;
import edu.nii.qlks.ControlLayer.LoaiKhachHangControl;
import edu.nii.qlks.ControlLayer.LoaiPhongControl;
import edu.nii.qlks.ControlLayer.PhieuDenControl;
import edu.nii.qlks.ControlLayer.PhongControl;
import edu.nii.qlks.ControlLayer.ThamSoControl;
import edu.nii.qlks.DataTransferObject.CTPhieuDenInfo;
import edu.nii.qlks.DataTransferObject.KhachHangInfo;
import edu.nii.qlks.DataTransferObject.PhieuDenInfo;
import edu.nii.qlks.DataTransferObject.PhongInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author MARS_NHO
 */
public class jfrm_LapPhieuThuePhong extends javax.swing.JInternalFrame {
    
    PhongControl phongCT;
    PhongInfo phongInfo;
    LoaiPhongControl loaiPhongCT;
    ThamSoControl thamSoCT;
    LoaiKhachHangControl loaiKhachHangCT;
    KhachHangControl khachHangCT;
    KhachHangInfo khachHangInfo;
    CTPhieuDenControl chiTietPhieuThueCT;
    CTPhieuDenInfo chiTietPhieuThueInfo;
    
    PhieuDenControl phieuThuePhongCT;
    PhieuDenInfo phieuThuePhongInfo;
    
    private static Vector tableRecords = new Vector();
    private static Vector tableTitle = new Vector();
    
    /**
     * Creates new form jfrm_LapPhieuThuePhong
     */
    public jfrm_LapPhieuThuePhong() {
        initComponents();
        
        phongCT = new PhongControl();
        phongInfo = new PhongInfo();
        loaiPhongCT = new LoaiPhongControl();
        thamSoCT = new ThamSoControl();
        phieuThuePhongCT = new PhieuDenControl();
        loaiKhachHangCT = new LoaiKhachHangControl();
        khachHangCT = new KhachHangControl();
        khachHangInfo = new KhachHangInfo();
        phieuThuePhongInfo = new PhieuDenInfo();
        chiTietPhieuThueCT = new CTPhieuDenControl();
        chiTietPhieuThueInfo = new CTPhieuDenInfo();
                
        tableTitle.add("STT");
        tableTitle.add("Tên khách hàng");
        tableTitle.add("Loại khách hàng");
        tableTitle.add("CMND");
        tableTitle.add("Địa chỉ");
        //jXTable1.setModel(new DefaultTableModel(tableRecords, tableTitle));
        
        Load_Table();
        try {
            Load_Form();
            Load_LoaiKhachHang_In_Table();
        } catch (Exception ex) {
            Logger.getLogger(jfrm_LapPhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        Test_Date();
    }
    
    public void Test_Date(){
        //RANG BUOC NGAY THANG THUE PHONG
        jXDatePicker_NgayThuePhong.setFormats("dd/MM/yyyy");
        jXDatePicker_NgayThuePhong.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Calendar date = Calendar.getInstance();
                if (jXDatePicker_NgayThuePhong.getDate().compareTo(date.getTime()) > 0){
                    JOptionPane.showMessageDialog(rootPane, "Không chọn ngày lập sau ngày hiện hành!");
                    jXDatePicker_NgayThuePhong.setDate(new Date());
                }
                //MOC NGAY LAP: 1/5/2013
                date.set(Calendar.YEAR, 2013);
                date.set(Calendar.MONTH, 4);
                date.set(Calendar.DAY_OF_MONTH, 1);
                if (jXDatePicker_NgayThuePhong.getDate().compareTo(date.getTime()) < 0){
                    JOptionPane.showMessageDialog(rootPane, "Không chọn ngày lập trước ngày: " + date.getTime());
                    jXDatePicker_NgayThuePhong.setDate(new Date());
                }
            }
        });
    }
    
    public void Load_Form() throws Exception{
        Vector vt = new Vector();
        vt = phongCT.LayDS("TinhTrangPhong = 0"); //LAY DS PHONG CHUA CHO THUE, TT= 0
        for (int i = 0; i < vt.size(); i++){
            Vector vt_row = new Vector();
            vt_row = (Vector) vt.get(i);
            String str = vt_row.get(1).toString().trim();
            jComboBox_TenPhong.addItem(str);
        }
        //TAO TINH NANG NHAP TU DONG TRONG COMBOBOX_TENPHONG
        AutoCompleteDecorator.decorate(jComboBox_TenPhong);
        
        
        
    }
    
    public void Load_Table(){
        Object[] add = {"1", null, null, null, null};
        ((DefaultTableModel) jXTable_KhachHang.getModel()).addRow(add);
        
        
    }
    
    public void Load_LoaiKhachHang_In_Table() throws Exception{
        jXTable_KhachHang.getColumnModel().getColumn(0).setPreferredWidth(5);
        jXTable_KhachHang.getColumnModel().getColumn(1).setPreferredWidth(150);
        jXTable_KhachHang.getColumnModel().getColumn(2).setPreferredWidth(50);
        jXTable_KhachHang.getColumnModel().getColumn(3).setPreferredWidth(50);
        jXTable_KhachHang.getColumnModel().getColumn(4).setPreferredWidth(100);
        
        //TAO COMBOBOX VAO TRUONG MA LOAI KHACH TRONG BANG
        Vector com_value = new Vector();
        JComboBox com = new JComboBox();
         com_value = loaiKhachHangCT.LayDS(); 
        for (int i = 0; i < com_value.size(); i++){
            Vector vt_row = new Vector();
            vt_row = (Vector) com_value.get(i);
            
            String str = vt_row.get(1).toString();
            com.addItem(str);
        }
        //TAO TINH NANG NHAP TU DONG TRONG COMBOBOX_LOAI KHACH HANG
        AutoCompleteDecorator.decorate(com);
        
        //ADD COMBOBOX VAO JXTABLE
        DefaultCellEditor dce = new DefaultCellEditor(com);
        jXTable_KhachHang.getColumnModel().getColumn(2).setCellEditor(dce);
    }
    
    public void Get_PhieuThuePhongInfo(){
        String str = "";
        Vector vt_ds = new Vector();
        try {
            vt_ds = phieuThuePhongCT.LayDS();
            if (vt_ds.size() > 0){
                Vector vt_row = new Vector();
                vt_row = (Vector) vt_ds.get(vt_ds.size() - 1);
                str = vt_row.get(0).toString().trim(); //ma phong              
                String matd = phieuThuePhongCT.TaoMaTuDong(str, 3);
                phieuThuePhongInfo.setMaPhieuThue(matd);
            }
            else{
                String matd = phieuThuePhongCT.TaoMaTuDong("MPT000", 3);
                phieuThuePhongInfo.setMaPhieuThue(matd);
            }
            System.out.println("ma phieu thue " + phieuThuePhongInfo.getMaPhieuThue());
            
            String tenPhong = jComboBox_TenPhong.getSelectedItem().toString().trim();
            Vector vt = phongCT.LayDonGiaPhong(tenPhong.trim());
            Vector vt_maphong = (Vector)vt.get(0);
            
            phieuThuePhongInfo.setMaPhong(vt_maphong.get(1).toString().trim()); //0: dongia, 1: maphong
            System.out.println("ma phong " + phieuThuePhongInfo.getMaPhong());
            
            java.sql.Date insertDate = new java.sql.Date(jXDatePicker_NgayThuePhong.getDate().getTime());
            phieuThuePhongInfo.setNgayThuePhong(insertDate);
            System.out.println("ngay thue " + phieuThuePhongInfo.getNgayThuePhong());
            
            phieuThuePhongInfo.setDonGiaThue(Float.parseFloat(jXTextField_DonGiaThue.getText().trim()));
            System.out.println("don gia thue: " + phieuThuePhongInfo.getDonGiaThue());
            
            phieuThuePhongInfo.setSoNgayThue(0);
            phieuThuePhongInfo.setThanhTien(0);
            phieuThuePhongInfo.setMaHoaDon("");
            
        } catch (Exception ex) {
            Logger.getLogger(LapDanhMucPhong.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }
    }
    
    public int Get_Count_KhachHang(){
        int rows_data = 0;
        for (int i = 0; i < jXTable_KhachHang.getRowCount(); i++){
            boolean isData = false;
            for (int j = 1; j < jXTable_KhachHang.getColumnCount(); j++) {
                //j = 0, STT, ta k xet
                try {
                    String temp = (String) jXTable_KhachHang.getValueAt(i, j);
                    System.out.println("....." + temp);
                    if (!temp.trim().equals("")) {
                        isData = true;
                    }
                } catch (Exception e) {
                }
            }
            if (isData){
                rows_data++;
                System.out.println("----------"+rows_data);
            }
        }
        return rows_data;
    }
    
     
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jXPanel_Phong = new org.jdesktop.swingx.JXPanel();
        jXLabel_TenPhong = new org.jdesktop.swingx.JXLabel();
        jXLabel_DonGiaThue = new org.jdesktop.swingx.JXLabel();
        jXLabel_NgayThuePhong = new org.jdesktop.swingx.JXLabel();
        jComboBox_TenPhong = new javax.swing.JComboBox();
        jXTextField_DonGiaThue = new org.jdesktop.swingx.JXTextField();
        jXDatePicker_NgayThuePhong = new org.jdesktop.swingx.JXDatePicker();
        jXLabel_TieuDe = new org.jdesktop.swingx.JXLabel();
        jXButton_LamMoi = new org.jdesktop.swingx.JXButton();
        jXButton_LapPhieu = new org.jdesktop.swingx.JXButton();
        jXButton_Thoat = new org.jdesktop.swingx.JXButton();
        jXPanel_KhachHang = new org.jdesktop.swingx.JXPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable_KhachHang = new org.jdesktop.swingx.JXTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Lập phiếu thuê phòng");
        setToolTipText("Lập phiếu thuê phòng");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jXPanel_Phong.setBackground(new java.awt.Color(255, 255, 255));
        jXPanel_Phong.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin phòng thuê", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jXPanel_Phong.setToolTipText("Nhập thông tin của phiếu thuê");
        jXPanel_Phong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXLabel_TenPhong.setText("Tên phòng");
        jXLabel_TenPhong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXLabel_DonGiaThue.setText("Đơn giá thuê");
        jXLabel_DonGiaThue.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXLabel_NgayThuePhong.setText("Ngày thuê phòng");
        jXLabel_NgayThuePhong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jComboBox_TenPhong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jComboBox_TenPhong.setToolTipText("Chọn tên phòng cần thuê");
        jComboBox_TenPhong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_TenPhongActionPerformed(evt);
            }
        });

        jXTextField_DonGiaThue.setEditable(false);
        jXTextField_DonGiaThue.setBackground(new java.awt.Color(255, 255, 255));
        jXTextField_DonGiaThue.setToolTipText("Đơn giá của phòng thuê");
        jXTextField_DonGiaThue.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXDatePicker_NgayThuePhong.setToolTipText("Chọn ngày thuê phòng");
        jXDatePicker_NgayThuePhong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        javax.swing.GroupLayout jXPanel_PhongLayout = new javax.swing.GroupLayout(jXPanel_Phong);
        jXPanel_Phong.setLayout(jXPanel_PhongLayout);
        jXPanel_PhongLayout.setHorizontalGroup(
            jXPanel_PhongLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel_PhongLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jXPanel_PhongLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXLabel_TenPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel_DonGiaThue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(jXPanel_PhongLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jXTextField_DonGiaThue, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox_TenPhong, 0, 151, Short.MAX_VALUE))
                .addGap(38, 38, 38)
                .addComponent(jXLabel_NgayThuePhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(jXDatePicker_NgayThuePhong, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jXPanel_PhongLayout.setVerticalGroup(
            jXPanel_PhongLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel_PhongLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel_PhongLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel_TenPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel_NgayThuePhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox_TenPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXDatePicker_NgayThuePhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(jXPanel_PhongLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel_DonGiaThue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXTextField_DonGiaThue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        jXLabel_TieuDe.setText("LẬP PHIẾU THUÊ PHÒNG");
        jXLabel_TieuDe.setToolTipText("");
        jXLabel_TieuDe.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

        jXButton_LamMoi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        jXButton_LamMoi.setText("Làm mới");
        jXButton_LamMoi.setToolTipText("Lập thêm phiếu thuê mới");
        jXButton_LamMoi.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jXButton_LamMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_LamMoiActionPerformed(evt);
            }
        });

        jXButton_LapPhieu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add.png"))); // NOI18N
        jXButton_LapPhieu.setText("Lập phiếu");
        jXButton_LapPhieu.setToolTipText("Lập phiếu thuê phòng");
        jXButton_LapPhieu.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jXButton_LapPhieu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_LapPhieuActionPerformed(evt);
            }
        });

        jXButton_Thoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit_16.png"))); // NOI18N
        jXButton_Thoat.setText("Thoát");
        jXButton_Thoat.setToolTipText("Thoát khỏi Lập phiếu thuê phòng");
        jXButton_Thoat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jXButton_Thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_ThoatActionPerformed(evt);
            }
        });

        jXPanel_KhachHang.setBackground(new java.awt.Color(255, 255, 255));
        jXPanel_KhachHang.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin khách hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jXPanel_KhachHang.setLayout(new java.awt.BorderLayout());

        jXTable_KhachHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Tên khách hàng", "Loại khách hàng", "CMND", "Địa chỉ"
            }
        ));
        jScrollPane1.setViewportView(jXTable_KhachHang);

        jXPanel_KhachHang.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(249, 249, 249)
                .addComponent(jXLabel_TieuDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(0, 40, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jXButton_LamMoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jXButton_LapPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(jXButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jXPanel_KhachHang, javax.swing.GroupLayout.DEFAULT_SIZE, 646, Short.MAX_VALUE)
                    .addComponent(jXPanel_Phong, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(79, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jXLabel_TieuDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(jXPanel_Phong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jXPanel_KhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXButton_LapPhieu, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                    .addComponent(jXButton_LamMoi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jXButton_Thoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jXButton_ThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_ThoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_jXButton_ThoatActionPerformed

    private void jXButton_LapPhieuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_LapPhieuActionPerformed
        if (jComboBox_TenPhong.getSelectedItem().toString().trim().equals("")){
            JOptionPane.showMessageDialog(rootPane, "Vui lòng chọn tên phòng!");
            jComboBox_TenPhong.requestFocus();
            return;
        }
        if (jXDatePicker_NgayThuePhong.getDate() == null){
            JOptionPane.showMessageDialog(rootPane, "Vui lòng chọn ngày lập phiếu!");
            jXDatePicker_NgayThuePhong.requestFocus();
            return;
        }
                
        Get_PhieuThuePhongInfo();
        
        //-----------THEM KHACH HANG VAO CSDL----------------//
        int rows_data = 0;
        rows_data = Get_Count_KhachHang();
        
        if (rows_data != 0){
            try {
                //Lay so khach toi da
                Vector vt_temp = thamSoCT.LayDS();
                Vector vt_row = (Vector)vt_temp.get(0);
                int soKhachToiDa = Integer.parseInt(vt_row.get(0).toString().trim());
                System.out.println("so khach hien tai " + rows_data);
                System.out.println("so khach toi da " + soKhachToiDa);
                if (rows_data > soKhachToiDa){
                    JOptionPane.showMessageDialog(rootPane, "Số khách tối đa là " + soKhachToiDa);
                    return;
                }
                for (int i = 0; i < rows_data; i++){
                    String field = "";
                    Vector khachhang_vt = new Vector();
                    khachhang_vt = khachHangCT.LayDS();
                    if (khachhang_vt.size() > 0){
                        Vector khachhang_row = (Vector)khachhang_vt.get(khachhang_vt.size() - 1);
                        field = khachhang_row.get(0).toString().trim();
                        String temp = khachHangCT.TaoMaTuDong(field, 3);
                        khachHangInfo.setMaKhachHang(temp);
                    }
                    else{
                        khachHangInfo.setMaKhachHang(khachHangCT.TaoMaTuDong("MKH000", 3));
                    }
                    System.out.println("ma khach hang: "+khachHangInfo.getMaKhachHang());
                    
                    boolean[] isField = new boolean[4];
                    String cmnd = "";
                    try{
                        isField[0] = isField[1] = isField[2] = isField[3] = false;
                        field = jXTable_KhachHang.getValueAt(i, 1).toString().trim();
                        isField[0] = true;
                        if (field.equals("")){
                            JOptionPane.showMessageDialog(rootPane, "Vui lòng nhập tên khách hàng!");
                            return;
                        }
                        System.out.println("ten khach hang: "+field);
                        
                        field = jXTable_KhachHang.getValueAt(i, 2).toString().trim();
                        isField[1] = true;
                        if (field.equals("")){
                            JOptionPane.showMessageDialog(rootPane, "Vui lòng chọn loại khách hàng!");
                            return;
                        }
                        System.out.println("loai khach hang: "+field+";;;;;;;;;;;");
                        
                        field = jXTable_KhachHang.getValueAt(i, 3).toString().trim();
                        isField[2] = true;
                        cmnd = field;
                        try {
                            if ((cmnd.length() > 9) || (cmnd.length() < 9)) {
                                JOptionPane.showMessageDialog(rootPane, "CMND không hợp lệ! CMND có 9 chữ số.");
                                return;
                            }
                            float CMND = Float.parseFloat(cmnd);
                        } catch (Exception ex) {
                            JOptionPane.showMessageDialog(rootPane, "CMND không hợp lệ!");
                            return;
                        }
                        System.out.println("CMND: "+field);
                        
                        field = jXTable_KhachHang.getValueAt(i, 4).toString().trim();
                        isField[3] = true;
                        System.out.println("Dia chi: "+field);
                    }
                    catch (Exception e){
                        if (!isField[0]) {
                            JOptionPane.showMessageDialog(rootPane, "Vui lòng nhập tên khách hàng");
                            return;
                        }
                        if (!isField[1]) {
                            JOptionPane.showMessageDialog(rootPane, "Vui lòng nhập loại khách hàng");                            
                            return;
                        }
                        if (!isField[2]) {
                            JOptionPane.showMessageDialog(rootPane, "Vui lòng nhập CMND của khách hàng");
                            return;
                        }
                        if (!isField[3]) {
                            JOptionPane.showMessageDialog(rootPane, "Vui lòng nhập địa chỉ của khách hàng");
                            return;
                        } 
                    }
                    
                    //SET THONG TIN KHACH HANG VAO KHACHHANGINFO
                    khachHangInfo.setTenKhachHang(jXTable_KhachHang.getValueAt(i, 1).toString().trim());
                    String tenlk = jXTable_KhachHang.getValueAt(i, 2).toString().trim();
                    System.out.println("...."+tenlk+":");
                    Vector temp = new Vector();
                    temp = loaiKhachHangCT.LayMaLoaiKhach("N'" + tenlk + "'");
                    //temp = loaiKhachHangCT.LayDS("TenLoaiKhach = 'Nội Địa'");
                    if (temp.size() <=0 ){
                        JOptionPane.showMessageDialog(rootPane, "Du lieu k co");
                        return;
                    }
                    Vector temp_row = new Vector();
                    temp_row = (Vector)temp.get(0);
                    khachHangInfo.setMaLoaiKhach(temp_row.get(0).toString().trim());
                    khachHangInfo.setCMND(jXTable_KhachHang.getValueAt(i, 3).toString().trim());
                    khachHangInfo.setDiaChi(jXTable_KhachHang.getValueAt(i, 4).toString().trim());
                    
                    khachHangCT.Insert(khachHangInfo);
                    System.out.println("Them khach hang thanh cong");
                    
                    //SET THONG TIN CT PHIEU THUE VAO CT_PHIEUTHUEINFO
                    vt_temp = chiTietPhieuThueCT.LayDS();
                    if (vt_temp.size() > 0){
                        Vector vt_CT = (Vector) vt_temp.get(vt_temp.size() - 1);
                        String ma = vt_CT.get(0).toString().trim();
                        String maCT = chiTietPhieuThueCT.TaoMaTuDong(ma, 4);
                        chiTietPhieuThueInfo.setMaChiTietPhieu(maCT);
                    }
                    else{
                        chiTietPhieuThueInfo.setMaChiTietPhieu(chiTietPhieuThueCT.TaoMaTuDong("MCTP000", 4));
                    }
                    chiTietPhieuThueInfo.setMaPhieuThue(phieuThuePhongInfo.getMaPhieuThue());
                    chiTietPhieuThueInfo.setMaKhachHang(khachHangInfo.getMaKhachHang());
                    chiTietPhieuThueCT.Insert(chiTietPhieuThueInfo);
                    System.out.println("Them chi tiet phieu thue thanh cong");
                }
                
                //UPDATE LAI TINH TRANG PHONG = DA THUE
                phongCT.Update_TinhTrangPhong("'"+phieuThuePhongInfo.getMaPhong()+"'", 1);
                
                phieuThuePhongCT.Insert(phieuThuePhongInfo);
                JOptionPane.showMessageDialog(rootPane, "Thêm phiếu thuê phòng thành công!");
                
                jXButton_LapPhieu.setEnabled(false);
                jComboBox_TenPhong.setEditable(false);
                jXDatePicker_NgayThuePhong.setEditable(false);
            } catch (Exception ex) {
                Logger.getLogger(jfrm_LapPhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }//GEN-LAST:event_jXButton_LapPhieuActionPerformed

    private void jComboBox_TenPhongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_TenPhongActionPerformed
        
        if (jComboBox_TenPhong.getItemCount() > 0){
            try {
                String str = jComboBox_TenPhong.getSelectedItem().toString().trim();
                Vector vt = phongCT.LayDonGiaPhong(str.trim());
                Vector vt_dongia = (Vector)vt.get(0);
                jXTextField_DonGiaThue.setText(vt_dongia.get(0).toString().trim());
            } catch (Exception ex) {
                //Logger.getLogger(jfrm_LapPhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }//GEN-LAST:event_jComboBox_TenPhongActionPerformed

    private void jXButton_LamMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_LamMoiActionPerformed
        jComboBox_TenPhong.setEditable(true);
        jComboBox_TenPhong.removeAllItems();
        jXTextField_DonGiaThue.setText("");
        jXButton_LapPhieu.setEnabled(true);
        jXDatePicker_NgayThuePhong.setEditable(true);
        jXTable_KhachHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null}
            },
            new String [] {
                "STT", "Tên khách hàng", "Loại khách hàng", "CMND", "Địa chỉ"
            }
        ));
        try {
            Load_Form();
            Load_LoaiKhachHang_In_Table();
        } catch (Exception ex) {
            Logger.getLogger(jfrm_LapPhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
        }
              
    }//GEN-LAST:event_jXButton_LamMoiActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox_TenPhong;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXButton jXButton_LamMoi;
    private org.jdesktop.swingx.JXButton jXButton_LapPhieu;
    private org.jdesktop.swingx.JXButton jXButton_Thoat;
    private org.jdesktop.swingx.JXDatePicker jXDatePicker_NgayThuePhong;
    private org.jdesktop.swingx.JXLabel jXLabel_DonGiaThue;
    private org.jdesktop.swingx.JXLabel jXLabel_NgayThuePhong;
    private org.jdesktop.swingx.JXLabel jXLabel_TenPhong;
    private org.jdesktop.swingx.JXLabel jXLabel_TieuDe;
    private org.jdesktop.swingx.JXPanel jXPanel_KhachHang;
    private org.jdesktop.swingx.JXPanel jXPanel_Phong;
    private org.jdesktop.swingx.JXTable jXTable_KhachHang;
    private org.jdesktop.swingx.JXTextField jXTextField_DonGiaThue;
    // End of variables declaration//GEN-END:variables
}
